<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>自动生成列表</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
</head>

<body>

  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item layui-row">
          <div class="layui-inline layui-col-md12">
            <label class="layui-form-label">数据库名</label>
            <div class="layui-input-inline-block layui-col-md4">
              <select name="dbName" id="dbName" lay-verify="required" lay-filter="dbNameFilter">
                <option value=""></option>
              </select>
            </div>
          </div>
          <div class="layui-inline layui-col-md12">
            <label class="layui-form-label">表名</label>
            <div class="layui-input-inline-block layui-col-md4">
              <select name="tableName" id="tableName" lay-verify="required" lay-filter="tableNameFilter">
                <option value=""></option>
              </select>
            </div>
          </div>

          <div class="layui-inline layui-col-md12">
            <label class="layui-form-label">模板路径</label>
            <div class="layui-input-inline-block layui-col-md4">
              <input type="text" name="templatePath" id="templatePath" placeholder="请输入模板路径" autocomplete="off" class="layui-input">
            </div>
          </div>

          <div class="layui-inline layui-col-md12">
            <label class="layui-form-label">包名</label>
            <div class="layui-input-inline-block layui-col-md4">
              <input type="text" name="packageName" id="packageName" placeholder="请输入包名" autocomplete="off" class="layui-input">
            </div>
          </div>


          <div class="layui-inline layui-col-md12">
            <label class="layui-form-label">java写入路径</label>
            <div class="layui-input-inline-block layui-col-md4">
              <input type="text" name="writePath" id="writePath" placeholder="请输入java写入路径" autocomplete="off" class="layui-input">
            </div>
          </div>

          <div class="layui-inline layui-col-md12">
            <label class="layui-form-label">vue写入路径</label>
            <div class="layui-input-inline-block layui-col-md4">
              <input type="text" name="vuePath" id="vuePath" placeholder="请输入vue写入路径" autocomplete="off" class="layui-input">
            </div>
          </div>

          <div class="layui-inline layui-col-md12">
           
              <div class="layui-row">
                <div  class="layui-col-lg2" >
                  <input type="checkbox" name="Mapper" title="Mapper">
                </div>
                <div  class="layui-col-lg2" >
                  <input type="checkbox" name="Dao" title="Dao">
                </div>
                <div  class="layui-col-lg2" >
                  <input type="checkbox" name="Service" title="Service">
                </div>
                <div  class="layui-col-lg2" >
                  <input type="checkbox" name="ServiceImpl" title="ServiceImpl">
                </div>
                <div  class="layui-col-lg2" >
                  <input type="checkbox" name="Entity" title="Entity">
                </div>
                <div  class="layui-col-lg2" >
                  <input type="checkbox" name="Controller" title="Controller">
                </div>

              </div>

              <div class="layui-row">
                <div  class="layui-col-lg2" >
                  <input type="checkbox" name="Field" title="Field">
                </div>

                <div  class="layui-col-lg2" >
                  <input type="checkbox" name="Add" title="Add">
                </div>
                <div  class="layui-col-lg2" >
                  <input type="checkbox" name="Edit" title="Edit">
                </div>
                <div  class="layui-col-lg2" >
                  <input type="checkbox" name="Index" title="Index">
                </div>
                <div  class="layui-col-lg2" >
                  <input type="checkbox" name="IndexJs" title="IndexJs">
                </div>
                <div  class="layui-col-lg2" >
                  <input type="checkbox" name="vueConfigJsNext" title="vueConfigJsNext">
                </div>
              </div>
          </div>

          <div class="layui-inline">
            <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search">
              一键生成
            </button>
          </div>
        </div>
      </div>

      <div class="layui-card-body">
  
        <script type="text/html" id="buttonTpl">
          {{#  if(d.status){ }}
            <button class="layui-btn layui-btn-xs">已发布</button>
          {{#  } else { }}
            <button class="layui-btn layui-btn-primary layui-btn-xs">待修改</button>
          {{#  } }}
        </script>
        <script type="text/html" id="table-content-list">
          <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
          <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
        </script>
      </div>
    </div>
  </div>

  <script src="../../../layuiadmin/layui/layui.js"></script>
  <script>
    layui.config({
      base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
      index: 'lib/index' //主入口模块
    }).use(['index', 'contlist', 'table'], function () {
      var table = layui.table
        ,$ = layui.$
        ,layer = layui.layer
        , form = layui.form;

      //localStorage.getItem("dbName");
      //localStorage.getItem("tableName");
      
      var templatePath = localStorage.getItem("templatePath");
      var writePath = localStorage.getItem("writePath");
      var vuePath = localStorage.getItem("vuePath");
      var packageName = localStorage.getItem("packageName");
      $("#templatePath").val(templatePath);
      $("#writePath").val(writePath);
      $("#vuePath").val(vuePath);
      $("#packageName").val(packageName);
      //监听搜索
      form.on('submit(LAY-app-contlist-search)', function (data) {
        var field = data.field;

        console.log("field:" +JSON.stringify(field) );

        //执行重载
        /*table.reload('LAY-app-content-list', {
          where: field
        });*/
        $.ajax({
          url: 'http://127.0.0.1:8081/api/generate/createService',
          contentType: 'application/json',
          dataType:"json", 
          type: 'post',
          data: JSON.stringify(field),
          beforeSend:function(a,bsdata){
            var data =  JSON.parse(bsdata.data);
            localStorage.setItem("dbName",data.dbName);
            localStorage.setItem("tableName",data.tableName);
            localStorage.setItem("templatePath",data.templatePath);
            localStorage.setItem("writePath",data.writePath);
            localStorage.setItem("vuePath",data.vuePath);
            localStorage.setItem("packageName",data.packageName);
          },
          error:function(data){

            console.log("error:" + data);

          },
          success: function (data) {
            //下面会提到这个data是什么值
            layer.open({
              type: 1
              ,offset: 1
              ,id: 'layerDemo1' //防止重复弹出
              ,content:  '<div style="padding: 20px 100px;">'+"操作成功" +'</div>'
              ,btn: '关闭'
              ,btnAlign: 'c' //按钮居中
              ,shade: 0 //不显示遮罩
              ,yes: function(){
                layer.closeAll();
              }
            });
          }
        })

      });

      var active = {
        batchdel: function () {
          var checkStatus = table.checkStatus('LAY-app-content-list')
            , checkData = checkStatus.data; //得到选中的数据

          if (checkData.length === 0) {
            return layer.msg('请选择数据');
          }

          layer.confirm('确定删除吗？', function (index) {

            //执行 Ajax 后重载
            /*
            admin.req({
              url: 'xxx'
              //,……
            });
            */
            table.reload('LAY-app-content-list');
            layer.msg('已删除');
          });
        },
        add: function () {
          layer.open({
            type: 2
            , title: '添加文章'
            , content: 'listform.html'
            , maxmin: true
            , area: ['550px', '550px']
            , btn: ['确定', '取消']
            , yes: function (index, layero) {
              //点击确认触发 iframe 内容中的按钮提交
              var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
              submit.click();
            }
          });
        }
      };

      $.ajax({
        url: 'http://127.0.0.1:8081/api/generate/getDbs',
        dataType: 'json',
        type: 'post',
        success: function (data) {
          console.log(data);//下面会提到这个data是什么值
          //使用循环遍历，给下拉列表赋值
          let index = 0;
          $.each(data.data, function (index, value) {
            console.log(value);
            //下拉菜单里添加元素
            $('#dbName').append(new Option(value, value));
          });
          //重新渲染 固定写法
          layui.form.render("select");
          /*var dbName = localStorage.getItem("dbName");
          var tableName = localStorage.getItem("tableName");

          console.log("dbName:" + dbName)
          console.log("tableName:" + tableName)
          //$("#dbName").find("option[value='"+dbName+"']").attr("selected",true);
          $("#dbName").val(dbName)
          //$("#tableName").find("option[value='"+tableName+"']").attr("selected",true);
          $("#tableName").val(tableName)
          layui.form.render("select");*/
        }
      })

      //添加事件监听
      form.on('select(dbNameFilter)', function (data) {
        var dbName = data.value;
        console.log(dbName);
        $('#tableName').find("option").remove();
        $.ajax({

          url: 'http://127.0.0.1:8081/api/generate/getTables',
          dataType: 'json',
          data: {
            "dbName": dbName
          },
          type: 'post',
          success: function (data) {
            var list = data.data;
            console.log("tanle data : " + list);
            $.each(list, function (index, value) {
              console.log(value);
              //下拉菜单里添加元素
              $('#tableName').append(new Option(value, value));
            });
            //重新渲染 固定写法
            layui.form.render("select");

          }

        });
      });

      $('.layui-btn.layuiadmin-btn-list').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
      });

    });
  </script>
</body>

</html>